package me.sdevil507.systemAssist.systemLog.service.impl;

import com.google.common.collect.Lists;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import me.sdevil507.supports.jpa.PageParamsDTO;
import me.sdevil507.supports.jpa.SortParamsDTO;
import me.sdevil507.systemAssist.systemLog.mapper.SystemErrorLogMapper;
import me.sdevil507.systemAssist.systemLog.po.QSystemErrorLogPo;
import me.sdevil507.systemAssist.systemLog.po.SystemErrorLogPo;
import me.sdevil507.systemAssist.systemLog.repository.SystemErrorLogRepository;
import me.sdevil507.systemAssist.systemLog.service.SystemErrorLogService;
import me.sdevil507.systemAssist.systemLog.dto.SystemErrorLogParamsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SystemErrorLogServiceImpl implements SystemErrorLogService {

    @Autowired
    private SystemErrorLogRepository systemErrorLogrepository;

    @Autowired
    private SystemErrorLogMapper systemErrorLogMapper;


    @Override
    public Page<SystemErrorLogPo> readAllByPage(SystemErrorLogParamsDto searchParamsDto, PageParamsDTO pageParamsDto, SortParamsDTO sortParamsDto) {
        QSystemErrorLogPo qSystemErrorLogPo = QSystemErrorLogPo.systemErrorLogPo;

        // 开启谓语查询条件
        Predicate predicate = qSystemErrorLogPo.id.isNotNull();

        // 获取查询参数
        String module = searchParamsDto.getOperModule();
        if (!StringUtils.isEmpty(module)) {
            predicate = ExpressionUtils.and(predicate, qSystemErrorLogPo.operModule.eq(module));
        }
        String operTerminal = searchParamsDto.getOperTerminal();
        if (!StringUtils.isEmpty(operTerminal)) {
            predicate = ExpressionUtils.and(predicate, qSystemErrorLogPo.operTerminal.eq(operTerminal));
        }
        String operClassify = searchParamsDto.getOperClassify();
        if (!StringUtils.isEmpty(operClassify)) {
            predicate = ExpressionUtils.and(predicate, qSystemErrorLogPo.operClassify.eq(operClassify));
        }
        Date startTime = searchParamsDto.getStartTime();
        Date endTime = searchParamsDto.getEndTime();
        if (null != startTime) {
            predicate = ExpressionUtils.and(predicate, qSystemErrorLogPo.createTime.goe(startTime));
        }
        if (null != endTime) {
            predicate = ExpressionUtils.and(predicate, qSystemErrorLogPo.createTime.loe(endTime));
        }
        // 排序
        Sort sort = sortParamsDto.getSort();
        if (null == sort) {
            sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime"), new Sort.Order(Sort.Direction.DESC, "createTime"));
        }

        // 执行返回
        Pageable pageable = pageParamsDto.getPageableWithSort(sort);

        return systemErrorLogrepository.findAll(predicate, pageable);
    }

    @Override
    public List<SystemErrorLogPo> readAll(SystemErrorLogParamsDto searchParamsDto, PageParamsDTO pageDto, SortParamsDTO sortDto) {
        QSystemErrorLogPo qSystemErrorLogPo = QSystemErrorLogPo.systemErrorLogPo;

        // 开启谓语查询条件
        Predicate predicate = qSystemErrorLogPo.id.isNotNull();

        // 获取查询参数pid
        String module = searchParamsDto.getUserName();
        if (!StringUtils.isEmpty(module)) {
            predicate = ExpressionUtils.and(predicate, qSystemErrorLogPo.userName.eq(module));
        }
        
        // 排序
        Sort sort = sortDto.getSort();
        if (null == sort) {
            sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime"), new Sort.Order(Sort.Direction.DESC, "uploadTime"));
        }
        return Lists.newArrayList(systemErrorLogrepository.findAll(predicate, sort).iterator());
    }

    @Override
    public SystemErrorLogPo readById(Long id) {
        return systemErrorLogrepository.findById(id).orElse(null);
    }

    @Override
    public void add(SystemErrorLogPo excepLog) {
        systemErrorLogrepository.save(excepLog);
    }
}
